﻿Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports Global_Visas_Business_Layer
Imports System.Data
Imports System.Text
Partial Public Class frmAdminChargeTypes
    Private intSaveNeworEdit As Integer
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmAdminChargeTypes_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        refreshList()
        Me.btnAddToGroup.IsEnabled = False
        Me.btnRemoveFromGroup.IsEnabled = False
    End Sub

    Private Sub refreshList()
        Try
            Me.grdChargeTypes.ItemsSource = Admin.ChargesTypesList
            Me.grdChargeTypes.Columns(0).IsVisible = False
            Me.grdChargeTypes.Columns(1).Width = New GridLength(150)
            Me.grdChargeTypes.Columns(2).Width = New GridLength(80)
            Me.grdChargeTypes.Columns(4).IsVisible = False

            Me.grdChargeGroups.ItemsSource = Admin.ChargeGroupsList
            Me.grdChargeGroups.Columns(0).IsVisible = False
            Me.grdChargeGroups.Columns(1).Width = New GridLength(150)
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminChargeTypes-refreshList")
            Exit Sub
        End Try
        
        'Me.grdWhereHear.Columns(1).Width = New GridLength(140)
        'Me.grdQualifications.Columns(3).IsVisible = False
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click
        intSaveNeworEdit = 1
        ClearControls()
     
        Me.btnAddNew.IsEnabled = False
        Me.btnSave.IsEnabled = True
        Me.btnEdit.IsEnabled = False
        'Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
        Me.txtChargeGroup.Focus()
    End Sub

    Private Sub ClearControls()
        Me.txtChargeGroup.Text = String.Empty
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click

        If txtChargeGroup.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("Please enter Charge Group.", CommonDialog.CustomDialogIcons.Information, "Error")
            txtChargeGroup.Focus()
            Exit Sub
       
        Else
            btnSave.IsEnabled = False
            Try
                Dim Sql As New StringBuilder
                Select Case intSaveNeworEdit
                    Case 1 'new one
                        Sql = New StringBuilder
                        With Sql
                            .Append("")
                            .Append("insert into A_ChargeGroups ")
                            .Append("  (ChargeGroup_Name)")
                            .Append(" Values(")
                            .Append(" '" & Me.txtChargeGroup.Text & "')")
                        End With

                        If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                            refreshList()
                            CommonFuncs.DisplayMessage("The Charge Group has been added successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                            'Exit Sub
                        Else
                            CommonFuncs.DisplayMessage("There is an error adding the Charge Group to the system. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                            'Exit Sub
                        End If
                    Case 2 'existing one
                        Sql = New StringBuilder
                        With Sql
                            .Append("update A_ChargeGroups set ")
                            .Append(" ChargeGroup_Name = '" & Me.txtChargeGroup.Text & "'")
                            Dim selectedGroup As DataRow = grdChargeGroups.SelectedItem
                            .Append(" where ChargeGroup_ID = " & selectedGroup(0))
                        End With

                        If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                            refreshList()
                            CommonFuncs.DisplayMessage("The Charge Group has been updated successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                            'Exit Sub
                        Else
                            CommonFuncs.DisplayMessage("There is an error in editing the Charge Group's details. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                            'Exit Sub
                        End If

                End Select

            Catch ex As Exception
                CommonFuncs.ErrorHandler(ex, "frmAdminChargeTypes-btnSave_click")
                Exit Sub
            End Try

        End If

        ClearControls()
        txtChargeGroup.IsEnabled = False
        btnAddNew.IsEnabled = True
        btnEdit.IsEnabled = False
        btnSave.IsEnabled = False
        'End If

    End Sub

    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        intSaveNeworEdit = 2
        'ClearControls()
        Me.txtChargeGroup.IsEnabled = True
        Dim selectedGroup As DataRow = grdChargeGroups.SelectedItem
        If Not selectedGroup Is Nothing Then
            Me.btnSave.IsEnabled = True
            ' Me.btnCancel.IsEnabled = True
            Me.btnDelete.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.txtChargeGroup.Text = selectedGroup(1).ToString()
            Me.txtChargeGroup.Focus()
        Else
            CommonFuncs.DisplayMessage("Selecet the Group to Edit", CommonDialog.CustomDialogIcons.Information, "Select")
        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
        Try
            Dim deleteOption As MsgBoxResult
            deleteOption = MsgBox("Are you really sure to delete This Charge Group ?", MsgBoxStyle.OkCancel)
            If deleteOption = MsgBoxResult.Ok Then
                Dim selectedRow As DataRow = grdChargeGroups.SelectedItem
                Dim Sql As New StringBuilder
                With Sql
                    .Append("delete  A_ChargeGroups where ChargeGroup_ID=" & selectedRow(0).ToString())
                End With

                If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                    btnDelete.IsEnabled = False
                    'btnCancel.IsEnabled = False
                    btnEdit.IsEnabled = False
                    btnSave.IsEnabled = False
                    'grdWhereHear.IsEnabled = True
                    ClearControls()
                    refreshList()
                    CommonFuncs.DisplayMessage("The Charge Group have been deleted successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                Else
                    CommonFuncs.DisplayMessage("There is an error while deleting the Charge Group from the system. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                    'Exit Sub
                End If

            ElseIf deleteOption = MsgBoxResult.Cancel Then

            End If
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminChargeTypes-btnDelete_Click")
            Exit Sub
        End Try
        
    End Sub

    Private Sub grdChargeGroups_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdChargeGroups.SelectionChanged
        ' Fill the respective charges in the group grdChargesInGroup
        grdChargesInGroup.ItemsSource = Nothing
        Dim selectedRow As DataRow = grdChargeGroups.SelectedItem
        If Not selectedRow Is Nothing Then
            FillChargeTypesOfGroup(selectedRow(0))
            Me.btnAddToGroup.IsEnabled = True
            Me.btnDelete.IsEnabled = True
            Me.btnEdit.IsEnabled = True
            Me.btnRemoveFromGroup.IsEnabled = True
        Else
            CommonFuncs.DisplayMessage("Please select a Charge Group !", CommonDialog.CustomDialogIcons.Information, "Select")
        End If

    End Sub

    Private Sub FillChargeTypesOfGroup(ByVal ChargeGroupId As Integer)
        Try
            grdChargesInGroup.ItemsSource = Nothing
            grdChargesInGroup.ItemsSource = Admin.ChargesInGroupList(ChargeGroupId)
            grdChargesInGroup.Columns(0).IsVisible = False
            grdChargesInGroup.Columns(1).IsVisible = False
            grdChargesInGroup.Columns(2).Width = New GridLength(220)
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminChargeTypes-FillChargeTypesOfGroup")
        End Try

    End Sub

    Private Sub grdChargeTypes_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdChargeTypes.SelectionChanged
        Me.btnAddToGroup.IsEnabled = True
    End Sub

    Private Sub grdChargesInGroup_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdChargesInGroup.SelectionChanged
        If Me.grdChargesInGroup.SelectedItems.Count > 0 Then
            Me.btnAddToGroup.IsEnabled = False
            Me.btnRemoveFromGroup.IsEnabled = True
            Me.btnEdit.IsEnabled = True
            Me.btnAddNew.IsEnabled = True
            Me.btnDelete.IsEnabled = True
            Me.txtChargeGroup.IsEnabled = False
        End If
    End Sub
    Private Sub AddChargeInGroup()
        Dim Sql As New StringBuilder
        Dim chargeGroupId, chargeTypeId As Integer
        Dim selectedChargeTypeRow As DataRow = grdChargeTypes.SelectedItem
        chargeTypeId = Convert.ToInt32(selectedChargeTypeRow(0))
        Dim selectedChargeGroupRow As DataRow = grdChargeGroups.SelectedItem
        chargeGroupId = selectedChargeGroupRow(0)
        With Sql
            .Append("insert into A_ChargeLinks(ChargeLink_ChargeTypeID,ChargeLink_ChargeGroupID) ")
            .Append(" Values( " & chargeTypeId & "," & chargeGroupId & ")")
        End With
        Try
            CommonDataFunctions.ExecuteSQL(Sql.ToString())
            FillChargeTypesOfGroup(chargeGroupId)

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminChargeTypes-btnAddToGroup_Click")
            Exit Sub
        End Try
    End Sub

    Private Sub btnAddToGroup_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddToGroup.Click
        If grdChargeGroups.SelectedItems.Count <> 0 Then
            Dim selectedChargeTypeRow As DataRow = grdChargeTypes.SelectedItem
            If Not selectedChargeTypeRow Is Nothing Then
                Dim chargesInGroup As DataTable = CType(grdChargesInGroup.ItemsSource, DataTable)
                If chargesInGroup.Rows.Count = 0 Then
                    AddChargeInGroup()
                End If
                For Each currentRow As DataRow In chargesInGroup.Rows
                    If Convert.ToInt32(currentRow(1)) = Convert.ToInt32(selectedChargeTypeRow(0)) Then
                        CommonFuncs.DisplayMessage("That Charge Type is already in this Charge Group", CommonDialog.CustomDialogIcons.Information, "")
                    Else
                        Dim Sql As New StringBuilder
                        Dim chargeGroupId, chargeTypeId As Integer
                        chargeTypeId = Convert.ToInt32(selectedChargeTypeRow(0))
                        Dim selectedChargeGroupRow As DataRow = grdChargeGroups.SelectedItem
                        chargeGroupId = selectedChargeGroupRow(0)
                        With Sql
                            .Append("insert into A_ChargeLinks(ChargeLink_ChargeTypeID,ChargeLink_ChargeGroupID) ")
                            .Append(" Values( " & chargeTypeId & "," & chargeGroupId & ")")
                        End With
                        Try
                            CommonDataFunctions.ExecuteSQL(Sql.ToString())
                            FillChargeTypesOfGroup(chargeGroupId)
                            Exit For
                        Catch ex As Exception
                            CommonFuncs.ErrorHandler(ex, "frmAdminChargeTypes-btnAddToGroup_Click")
                            Exit Sub
                        End Try

                        ' Add charge type to group and rebind the grdChargesInGroup

                    End If
                Next
            Else
                CommonFuncs.DisplayMessage("Please select a Charge Type to Add to A group.", CommonDialog.CustomDialogIcons.Information, "Select")
            End If
        Else : CommonFuncs.DisplayMessage("Charge Group must be Selected First to add the Charge Type", CommonDialog.CustomDialogIcons.Information, "Error")
        End If

    End Sub

    Private Sub btnRemoveFromGroup_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnRemoveFromGroup.Click
        Dim selectedChargeRow As DataRow = grdChargesInGroup.SelectedItem
        If Not selectedChargeRow Is Nothing Then
            Dim Sql As New StringBuilder
            With Sql
                .Append("Delete A_ChargeLinks Where ChargeLink_ID=" & Convert.ToInt32(selectedChargeRow(0)))
            End With
            Try
                CommonDataFunctions.ExecuteSQL(Sql.ToString())
            Catch ex As Exception
                CommonFuncs.ErrorHandler(ex, "frmAdminChargeTypes-btnRemoveFromGroup_Click")
                Exit Sub
            End Try
            Dim selectedChargeGroupRow As DataRow = grdChargeGroups.SelectedItem
            Dim chargeGroupId As Integer = selectedChargeGroupRow(0)
            FillChargeTypesOfGroup(chargeGroupId)
        Else
            CommonFuncs.DisplayMessage("Select the Charge Type to be removed first.", CommonDialog.CustomDialogIcons.Information, "Select")
        End If
    End Sub



    Private Sub btnDeleteChargeType_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDeleteChargeType.Click
        Dim selectedChargeTypeRow As DataRow = grdChargeTypes.SelectedItem
        If Not selectedChargeTypeRow Is Nothing Then
            Dim deleteOption As MsgBoxResult
            deleteOption = MsgBox("Are you really sure to delete This Charge Type ?", MsgBoxStyle.OkCancel)
            If deleteOption = MsgBoxResult.Ok Then
                Dim Sql As New StringBuilder
                With Sql
                    .Append("delete  A_ChargeTypes where ChargeType_ID=" & selectedChargeTypeRow(0))
                End With
                Try
                    If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                        refreshList()
                        CommonFuncs.DisplayMessage("The Charge Type has been deleted successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                    Else
                        CommonFuncs.DisplayMessage("There is an error while deleting the Charge Type from the system. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                        'Exit Sub
                    End If
                Catch ex As Exception
                    CommonFuncs.ErrorHandler(ex, "frmAdminChargeTypes-btnDeleteChargeType_Click")
                    Exit Sub
                End Try


            ElseIf deleteOption = MsgBoxResult.Cancel Then

            End If
        Else
            CommonFuncs.DisplayMessage("Select the Charge Type to be deleted", CommonDialog.CustomDialogIcons.Information, "Select")
        End If
    End Sub

    Private Sub btnAddChargeType_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddChargeType.Click
        Dim FormAddEditChargeType As New frmAdminEditChargeTypes()
        FormAddEditChargeType.ButtonVisibile = "Normal"
        FormAddEditChargeType.SaveNewOrEdit = 1
        FormAddEditChargeType.Tax = True
        FormAddEditChargeType.FormTitle = "Add Charge Type"
        FormAddEditChargeType.TextChargeType.Text = String.Empty
        FormAddEditChargeType.TextChargeType.Focus()
        FormAddEditChargeType.ShowDialog()
        refreshList()
    End Sub

    Private Sub btnEditChargeType_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEditChargeType.Click
        Dim selectedchargeRow As DataRow = grdChargeTypes.SelectedItem
        If Not selectedchargeRow Is Nothing Then
            Dim FormAddEditChargeType As New frmAdminEditChargeTypes()
            FormAddEditChargeType.FormTitle = "Edit Charge Type"
            If selectedchargeRow(4).ToString() = String.Empty Then
                FormAddEditChargeType.ChargeCategory = 0
            Else
                FormAddEditChargeType.ChargeCategory = Convert.ToInt32(selectedchargeRow(4))
            End If

            If Convert.ToInt32(selectedchargeRow(2)) = 0 Then
                FormAddEditChargeType.Tax = False
                FormAddEditChargeType.IsTaxZero = 0
            Else
                FormAddEditChargeType.Tax = True
                FormAddEditChargeType.IsTaxZero = selectedchargeRow(2)
                FormAddEditChargeType.TextTax.Text = selectedchargeRow(2)
            End If
            FormAddEditChargeType.SaveNewOrEdit = 2
            FormAddEditChargeType.ChargeTypeId = selectedchargeRow(0)
            FormAddEditChargeType.TextChargeType.Text = selectedchargeRow(1)
            FormAddEditChargeType.TextChargeType.Focus()
            FormAddEditChargeType.ShowDialog()
            refreshList()
        Else
            CommonFuncs.DisplayMessage("Selecet a Charge Type to edit.", CommonDialog.CustomDialogIcons.Information, "Select")
        End If

    End Sub
End Class
